package cn.stylefeng.guns.modular.system.service.impl;

import cn.hutool.core.convert.Convert;
import cn.stylefeng.guns.base.pojo.node.ZTreeNode;
import cn.stylefeng.guns.base.pojo.page.LayuiPageFactory;
import cn.stylefeng.guns.base.pojo.page.LayuiPageInfo;
import cn.stylefeng.guns.modular.system.entity.UserAreaRelation;
import cn.stylefeng.guns.modular.system.mapper.UserAreaRelationMapper;
import cn.stylefeng.guns.modular.system.model.params.UserAreaRelationParam;
import cn.stylefeng.guns.modular.system.model.result.UserAreaRelationResult;
import  cn.stylefeng.guns.modular.system.service.UserAreaRelationService;
import cn.stylefeng.guns.sys.modular.system.entity.Relation;
import cn.stylefeng.guns.sys.modular.system.service.DeptService;
import cn.stylefeng.roses.core.util.ToolUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.Serializable;
import java.util.List;

/**
 * <p>
 * 角色和菜单关联表 服务实现类
 * </p>
 *
 * @author 小飞
 * @since 2019-08-03
 */
@Service
public class UserAreaRelationServiceImpl extends ServiceImpl<UserAreaRelationMapper, UserAreaRelation> implements UserAreaRelationService {

    @Autowired
    private DeptService deptService;
    @Override
    public void add(UserAreaRelationParam param){
        UserAreaRelation entity = getEntity(param);
        this.save(entity);
    }

    @Override
    public void delete(UserAreaRelationParam param){
        this.removeById(getKey(param));
    }

    @Override
    public void update(UserAreaRelationParam param){
        UserAreaRelation oldEntity = getOldEntity(param);
        UserAreaRelation newEntity = getEntity(param);
        ToolUtil.copyProperties(newEntity, oldEntity);
        this.updateById(newEntity);
    }

    @Override
    public UserAreaRelationResult findBySpec(UserAreaRelationParam param){
        return null;
    }

    @Override
    public List<UserAreaRelationResult> findListBySpec(UserAreaRelationParam param){
        return null;
    }

    @Override
    public LayuiPageInfo findPageBySpec(UserAreaRelationParam param){
        Page pageContext = getPageContext();
        IPage page = this.baseMapper.customPageList(pageContext, param);
        List<UserAreaRelationResult> userAreaRelationResults = page.getRecords();
        userAreaRelationResults.stream().filter(s -> ToolUtil.isNotEmpty(deptService.getById(s.getDeptId()))).forEach(s -> {
            s.setDeptName(deptService.getById(s.getDeptId()).getSimpleName());
        });
        page.setRecords(userAreaRelationResults);
        return LayuiPageFactory.createPageInfo(page);
    }

    @Override
    public List<ZTreeNode> menuTreeListByMenuIds(List<Long> areaId) {
        return this.baseMapper.areaUserList(areaId);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void setAuthority(Long userId, String ids) {

        //删除当前用户所有的记录
         this.getBaseMapper().delete(new QueryWrapper<UserAreaRelation>().lambda().eq(UserAreaRelation::getUserId,userId));

        // 添加新的权限
        for (Long id : Convert.toLongArray(ids.split(","))) {
            UserAreaRelation userAreaRelation = new UserAreaRelation();
            userAreaRelation.setAddressId(id);
            userAreaRelation.setUserId(userId);
            this.getBaseMapper().insert(userAreaRelation);
        }
    }

    private Serializable getKey(UserAreaRelationParam param){
        return param.getId();
    }

    private Page getPageContext() {
        return LayuiPageFactory.defaultPage();
    }

    private UserAreaRelation getOldEntity(UserAreaRelationParam param) {
        return this.getById(getKey(param));
    }

    private UserAreaRelation getEntity(UserAreaRelationParam param) {
        UserAreaRelation entity = new UserAreaRelation();
        ToolUtil.copyProperties(param, entity);
        return entity;
    }

}
